﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.HeDaoTaoService;
namespace eTMS.Controllers{
public class HeDaoTaoControllerBase : CommonController{
	public HeDaoTaoControllerBase()
		: base()
	{	
		this.dao = new HeDaoTaoServiceClient();
	}
	~HeDaoTaoControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private HeDaoTaoEntity oHeDaoTao;	
	public HeDaoTaoEntity HeDaoTaoObject{
		get{
			return oHeDaoTao;
		}
		set{
			oHeDaoTao=value;
		}
	}
	protected HeDaoTaoServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSHeDaoTao(false);			
			return _Table;
		}
	}
	public void LoadAllDSHeDaoTao(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oHeDaoTao==null)return false;
		bool result=true;
		try{
			oHeDaoTao = dao.SaveRet(oHeDaoTao);
		}
		catch{
			result=false;
		}
		if(result){
			_ListHeDaoTao.Add(oHeDaoTao);
		}
		return result;
	}
	public bool Update(){
		if(oHeDaoTao==null)return false;
		return dao.Update(oHeDaoTao);
	}
	public bool Delete(){
		if(oHeDaoTao==null)return false;
		bool retvalue = dao.Delete(oHeDaoTao);
		if(retvalue){			
			_ListHeDaoTao.Remove(oHeDaoTao);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 HeDaoTao_ID){
		bool retvalue = dao.DeleteByID(HeDaoTao_ID);		
		if(retvalue){
			HeDaoTaoEntity oDeleted=FindHeDaoTao(HeDaoTao_ID);
			if(oDeleted!=null){
				_ListHeDaoTao.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oHeDaoTao = new HeDaoTaoEntity();
	}
	//Check if exists MaHeDaoTao
	public bool CheckExistsMaHeDaoTao(object value){	
		return dao.SelectOneByMaHeDaoTao(value.ToString()).MaHeDaoTao!=null;
    }    
    //Copy data from data row in grid to object
    public HeDaoTaoEntity MapDataRowToObject(DataRow row){
		HeDaoTaoEntity o = new HeDaoTaoEntity();
		 o.MaHeDaoTao=Convert.ToString(row["MaHeDaoTao"]);
		 o.TenHeDaoTao=Convert.ToString(row["TenHeDaoTao"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("HeDaoTao_ID"))
		    o.HeDaoTao_ID=(Int32)(row["HeDaoTao_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(HeDaoTaoEntity o,DataRow row){
		row["MaHeDaoTao"]=o.MaHeDaoTao;
		row["TenHeDaoTao"]=o.TenHeDaoTao;
		row["GhiChu"]=o.GhiChu;
		row["HeDaoTao_ID"]=o.HeDaoTao_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.HeDaoTaoObject,row);
    }
	public void LoadObject(Int32 HeDaoTao_ID){
		HeDaoTaoEntity oLoad=FindHeDaoTao(HeDaoTao_ID);
		if(oLoad==null){
			oHeDaoTao=this.LoadHeDaoTao(HeDaoTao_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oHeDaoTao.IsNullHeDaoTao_ID;
			if(!bNotIsNull){
				this.ListHeDaoTao.Add(oHeDaoTao);
			}
		}
		else{
			oHeDaoTao=oLoad;
		}
	}
	#region CachedList HeDaoTao
	private static List<HeDaoTaoEntity> _ListHeDaoTao=new List<HeDaoTaoEntity>();
    public List<HeDaoTaoEntity>ListHeDaoTao{
		get{
			return _ListHeDaoTao;
		}
	}
	public static List<HeDaoTaoEntity>ListHeDaoTaoStatic{
		get{
			return _ListHeDaoTao;
		}
	}
	public HeDaoTaoEntity FindHeDaoTao(Int32 HeDaoTao_ID){
		HeDaoTaoEntity oResult=this.ListHeDaoTao.Find(delegate(HeDaoTaoEntity oFind){
			bool result=true; 
			result&=oFind.HeDaoTao_ID.Equals(HeDaoTao_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllHeDaoTao(bool clearCache){
		if(_ListHeDaoTao.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		HeDaoTaoServiceClient dao = new HeDaoTaoServiceClient();
		_ListHeDaoTao=new List<HeDaoTaoEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public HeDaoTaoEntity LoadHeDaoTao(Int32 HeDaoTao_ID){			
		HeDaoTaoEntity o=dao.SelectOne(HeDaoTao_ID);		
		return o;
	}
	#endregion
}
}
